"""
cron: 0 * * * * ?
cron: 30 * * * * ?
new Env('5g测试');
"""
from Enty.vod5g import vod5g
from Tool.NetTool import GetHttpclient
from Tool.Path import get_env
from Tool.MysqlTool import MySQLHelper

Test = ""


def GetUrl():
    if Test:
        return ""
    else:
        return get_env(name="url5g", split=False)


def GetMySQLHelper():
    if Test:
        return MySQLHelper('1panel.offices', 'root', 'mysql_GRZwcG')
    else:
        mySqlHost = get_env(name="mysqloffice", split=True)
        return MySQLHelper(*mySqlHost)


def GetStep():
    if Test:
        return 1
    else:
        mySqlHost = get_env(name="step", split=False)
        return int(mySqlHost)


if __name__ == '__main__':
    MySQLHelper = GetMySQLHelper()
    MySQLHelper.setDB('G5vod')

    sql = "select count(*) from vod5g"
    rows = MySQLHelper.queryAll(sql)
    print("共计收录视频：" + str(rows[0]['count(*)']))

    step = GetStep()
    for i in range(step):
        print(f"正在进行任务 {i + 1} / {step}")
        sql = "select id from vod5g"
        rows = MySQLHelper.queryAll(sql)
        needId = 0
        if len(rows) == 0:
            needId = 1
        else:
            sql = (
                "select MIN(t1.id +1) AS missing_id FROM vod5g t1 LEFT JOIN vod5g t2 ON t1.id +1 = t2.id WHERE t2.id IS"
                " NULL")
            rows = MySQLHelper.queryAll(sql)
            needId = rows[0]['missing_id']
        print("正在收录id：" + str(needId))
        text = GetHttpclient(f'{GetUrl()}' + str(needId))
        data = vod5g(text)
        if data.id == 0:
            dataSource = {"id": str(needId), "title": '', "litpic": '', "typename": '',
                "m3u8url": '', "tag": '', "body": '', "series": ''}
        else:
            dataSource = data.insert()
        MySQLHelper.insert("vod5g", dataSource)
    MySQLHelper.close()
